using UnityEngine;
using QFramework;
using QFramework.Example;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

namespace ProjectSurvivor
{
	public partial class GetAllExp : GameplayObject
	{
		static IEnumerator FlyToPlayerStart()
		{
			IEnumerable<PowerUp>exps=FindObjectsByType<Exp>(FindObjectsInactive.Exclude,FindObjectsSortMode.None);
			IEnumerable<PowerUp>coins=FindObjectsByType<Coin>(FindObjectsInactive.Exclude,FindObjectsSortMode.None);
			int count=0;
			foreach(var powerUp in exps.Concat(coins)
			.OrderByDescending(e=>e.InScreen)
			.ThenBy(e=>e.Distance2D(Player.Default)))
			{
				if (powerUp.InScreen)
				{
										if(count>10)
					{
						yield return new WaitForEndOfFrame();
						count=0;
					}

				}
				else
				{
					if(count>5)
					{
						yield return new WaitForEndOfFrame();
						count=0;
					}
				}
				count++;
				ActionKit.OnUpdate.Register(() =>
					{
						var player = Player.Default;
						if (player)
						{
							
							powerUp.transform.Translate(player.NormalizedDirection2DFrom(powerUp).normalized * Time.deltaTime * 5f);
						}
					}).UnRegisterWhenGameObjectDestroyed(powerUp);

			}
			
			
		}
		void OnTriggerEnter2D(Collider2D other)
		{
			if (other.GetComponent<CollectableArea>())
			{
				PowerUpManager.Default.StartCoroutine(FlyToPlayerStart());

				AudioKit.PlaySound("GetAllExp");
				//删除
				this.DestroyGameObjGracefully();
			}

		}
		protected override Collider2D Collider2D => SelfCollider2D;
	}
}
